# -*- coding: utf-8 -*-
import asyncio
from multiprocessing import Process
from threading import Thread

################################################################################
## Form generated from reading UI file 'motor.ui'
##
## Created by: Qt User Interface Compiler version 6.7.1
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################

from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
                            QMetaObject, QObject, QPoint, QRect,
                            QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
                           QFont, QFontDatabase, QGradient, QIcon,
                           QImage, QKeySequence, QLinearGradient, QPainter,
                           QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QHBoxLayout, QLabel, QLineEdit,
                               QMainWindow, QMenuBar, QPushButton, QSizePolicy,
                               QStatusBar, QVBoxLayout, QWidget)

from src.action.motor_action import MotorAction


class Motor(object):
    def __init__(self):
        self.motor_action = MotorAction()

    def setupUi(self, MotorControl):
        if not MotorControl.objectName():
            MotorControl.setObjectName(u"MotorControl")
        MotorControl.resize(800, 600)
        self.centralwidget = QWidget(MotorControl)
        self.centralwidget.setObjectName(u"centralwidget")
        self.verticalLayoutWidget = QWidget(self.centralwidget)
        self.verticalLayoutWidget.setObjectName(u"verticalLayoutWidget")
        self.verticalLayoutWidget.setGeometry(QRect(60, 60, 301, 381))
        self.fixed_length_run = QVBoxLayout(self.verticalLayoutWidget)
        self.fixed_length_run.setSpacing(5)
        self.fixed_length_run.setObjectName(u"fixed_length_run")
        self.fixed_length_run.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout = QHBoxLayout()
        self.horizontalLayout.setObjectName(u"horizontalLayout")
        self.fixed_length_label = QLabel(self.verticalLayoutWidget)
        self.fixed_length_label.setObjectName(u"fixed_length_label")
        self.fixed_length_label.setAlignment(Qt.AlignCenter)

        self.horizontalLayout.addWidget(self.fixed_length_label)

        self.fixed_length_run.addLayout(self.horizontalLayout)

        self.decelerate = QHBoxLayout()
        self.decelerate.setObjectName(u"decelerate")
        self.label_3 = QLabel(self.verticalLayoutWidget)
        self.label_3.setObjectName(u"label_3")

        self.decelerate.addWidget(self.label_3)

        self.decelerate_input = QLineEdit(self.verticalLayoutWidget)
        self.decelerate_input.setObjectName(u"decelerate_input")

        self.decelerate.addWidget(self.decelerate_input)

        self.fixed_length_run.addLayout(self.decelerate)

        self.accelerate = QHBoxLayout()
        self.accelerate.setObjectName(u"accelerate")
        self.label_2 = QLabel(self.verticalLayoutWidget)
        self.label_2.setObjectName(u"label_2")

        self.accelerate.addWidget(self.label_2)

        self.accelerate_input_0 = QLineEdit(self.verticalLayoutWidget)
        self.accelerate_input_0.setObjectName(u"accelerate_input_0")

        self.accelerate.addWidget(self.accelerate_input_0)

        self.fixed_length_run.addLayout(self.accelerate)

        self.trip = QHBoxLayout()
        self.trip.setObjectName(u"trip")
        self.label_4 = QLabel(self.verticalLayoutWidget)
        self.label_4.setObjectName(u"label_4")

        self.trip.addWidget(self.label_4)

        self.trip_input = QLineEdit(self.verticalLayoutWidget)
        self.trip_input.setObjectName(u"trip_input")

        self.trip.addWidget(self.trip_input)

        self.fixed_length_run.addLayout(self.trip)

        self.speed = QHBoxLayout()
        self.speed.setObjectName(u"speed")
        self.label = QLabel(self.verticalLayoutWidget)
        self.label.setObjectName(u"label")

        self.speed.addWidget(self.label)

        self.speed_input = QLineEdit(self.verticalLayoutWidget)
        self.speed_input.setObjectName(u"speed_input")

        self.speed.addWidget(self.speed_input)

        self.fixed_length_run.addLayout(self.speed)

        self.start_stop = QHBoxLayout()
        self.start_stop.setObjectName(u"start_stop")
        self.start_button = QPushButton(self.verticalLayoutWidget)
        self.start_button.setObjectName(u"start_button")
        # 点击开始运行
        self.start_button.clicked.connect(self.fixed_length_start_run)

        self.start_stop.addWidget(self.start_button)

        self.stop_button = QPushButton(self.verticalLayoutWidget)
        self.stop_button.setObjectName(u"stop_button")
        # 点击急停
        self.stop_button.clicked.connect(self.click_emergency_stop)

        self.start_stop.addWidget(self.stop_button)

        self.fixed_length_run.addLayout(self.start_stop)

        self.verticalLayoutWidget_2 = QWidget(self.centralwidget)
        self.verticalLayoutWidget_2.setObjectName(u"verticalLayoutWidget_2")
        self.verticalLayoutWidget_2.setGeometry(QRect(410, 60, 311, 381))
        self.fixed_length_run_2 = QVBoxLayout(self.verticalLayoutWidget_2)
        self.fixed_length_run_2.setSpacing(5)
        self.fixed_length_run_2.setObjectName(u"fixed_length_run_2")
        self.fixed_length_run_2.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout = QVBoxLayout()
        self.verticalLayout.setObjectName(u"verticalLayout")
        self.fixed_length_label_2 = QLabel(self.verticalLayoutWidget_2)
        self.fixed_length_label_2.setObjectName(u"fixed_length_label_2")
        self.fixed_length_label_2.setEnabled(True)
        self.fixed_length_label_2.setAlignment(Qt.AlignCenter)

        self.verticalLayout.addWidget(self.fixed_length_label_2)

        self.fixed_length_run_2.addLayout(self.verticalLayout)

        self.decelerate_2 = QHBoxLayout()
        self.decelerate_2.setObjectName(u"decelerate_2")
        self.label_5 = QLabel(self.verticalLayoutWidget_2)
        self.label_5.setObjectName(u"label_5")

        self.decelerate_2.addWidget(self.label_5)

        self.decelerate_input_2 = QLineEdit(self.verticalLayoutWidget_2)
        self.decelerate_input_2.setObjectName(u"decelerate_input_2")

        self.decelerate_2.addWidget(self.decelerate_input_2)

        self.fixed_length_run_2.addLayout(self.decelerate_2)

        self.accelerate_2 = QHBoxLayout()
        self.accelerate_2.setObjectName(u"accelerate_2")
        self.label_6 = QLabel(self.verticalLayoutWidget_2)
        self.label_6.setObjectName(u"label_6")

        self.accelerate_2.addWidget(self.label_6)

        self.accelerate_input = QLineEdit(self.verticalLayoutWidget_2)
        self.accelerate_input.setObjectName(u"accelerate_input")

        self.accelerate_2.addWidget(self.accelerate_input)

        self.fixed_length_run_2.addLayout(self.accelerate_2)

        self.speed_2 = QHBoxLayout()
        self.speed_2.setObjectName(u"speed_2")
        self.label_8 = QLabel(self.verticalLayoutWidget_2)
        self.label_8.setObjectName(u"label_8")

        self.speed_2.addWidget(self.label_8)

        self.speed_input_2 = QLineEdit(self.verticalLayoutWidget_2)
        self.speed_input_2.setObjectName(u"speed_input_2")

        self.speed_2.addWidget(self.speed_input_2)

        self.fixed_length_run_2.addLayout(self.speed_2)

        self.start_stop_2 = QHBoxLayout()
        self.start_stop_2.setObjectName(u"start_stop_2")
        self.light = QPushButton(self.verticalLayoutWidget_2)
        self.light.setObjectName(u"light")
        self.light.clicked.connect(self.click_back)

        self.start_stop_2.addWidget(self.light)

        self.right = QPushButton(self.verticalLayoutWidget_2)
        self.right.setObjectName(u"right")
        self.right.clicked.connect(self.click_forward)

        self.start_stop_2.addWidget(self.right)

        # self.fixed_length_run_2.addLayout(self.start_stop_2)

        self.emergency_stop = QPushButton(self.verticalLayoutWidget_2)
        self.emergency_stop.setObjectName(u"emergency_stop")
        self.emergency_stop.clicked.connect(self.click_emergency_stop)

        self.start_stop_2.addWidget(self.emergency_stop)
        self.fixed_length_run_2.addLayout(self.start_stop_2)

        MotorControl.setCentralWidget(self.centralwidget)
        self.menubar = QMenuBar(MotorControl)
        self.menubar.setObjectName(u"menubar")
        self.menubar.setGeometry(QRect(0, 0, 800, 25))
        MotorControl.setMenuBar(self.menubar)
        self.statusbar = QStatusBar(MotorControl)
        self.statusbar.setObjectName(u"statusbar")
        MotorControl.setStatusBar(self.statusbar)

        self.retranslateUi(MotorControl)

        QMetaObject.connectSlotsByName(MotorControl)

    # setupUi

    def retranslateUi(self, MotorControl):
        MotorControl.setWindowTitle(
            # 电机控制上位机
            QCoreApplication.translate("MotorControl", u"\u7535\u673a\u63a7\u5236\u4e0a\u4f4d\u673a", None))
        # 定长运动
        self.fixed_length_label.setText(
            QCoreApplication.translate("MotorControl", u"\u5b9a\u957f\u5f80\u590d\u8fd0\u52a8", None))
        # 减速度：
        self.label_3.setText(QCoreApplication.translate("MotorControl", u"\u51cf\u901f\u5ea6\uff1a", None))
        # 加速度：
        self.label_2.setText(QCoreApplication.translate("MotorControl", u"\u52a0\u901f\u5ea6\uff1a", None))
        # 行程：
        self.label_4.setText(QCoreApplication.translate("MotorControl", u"\u884c\u7a0b\uff1a", None))
        # 速度：
        self.label.setText(QCoreApplication.translate("MotorControl", u"\u901f\u5ea6\uff1a", None))
        # 开始
        self.start_button.setText(QCoreApplication.translate("MotorControl", u"\u5f00\u59cb", None))
        # 急停
        self.stop_button.setText(QCoreApplication.translate("MotorControl", u"\u6025\u505c", None))

        #################右边控制页########################
        # 点动校验
        self.fixed_length_label_2.setText(QCoreApplication.translate("MotorControl", u"\u70b9\u52a8\u6821\u9a8c", None))
        # 减速度
        self.label_5.setText(QCoreApplication.translate("MotorControl", u"\u51cf\u901f\u5ea6\uff1a", None))
        # 加速度
        self.label_6.setText(QCoreApplication.translate("MotorControl", u"\u52a0\u901f\u5ea6\uff1a", None))
        # 速度
        self.label_8.setText(QCoreApplication.translate("MotorControl", u"\u901f\u5ea6\uff1a", None))
        # 向左
        self.light.setText(QCoreApplication.translate("MotorControl", u"\u5411\u5de6", None))
        # 向右
        self.right.setText(QCoreApplication.translate("MotorControl", u"\u5411\u53f3", None))
        # 急停
        self.emergency_stop.setText(QCoreApplication.translate("MotorControl", u"\u6025\u505c", None))

    # retranslateUi

    def point_run(self):
        """
        点动
        :return:
        """
        pass

    def fixed_length_start_run(self):
        """
        定长运动点击开始运行
        :return:
        """
        # 减速度
        decelerate_value = self.decelerate_input.text().strip()
        # 加速度
        accelerate_value = self.accelerate_input_0.text().strip()
        # 行程
        trip_value = self.trip_input.text().strip()
        # 速度
        speed_value = self.speed_input.text().strip()
        print(f"{trip_value}-{speed_value}-{accelerate_value}-{decelerate_value}")

        # motor_action = MotorAction()
        self.motor_action.fixed_length_reciprocate_motion(int(trip_value), int(speed_value), int(accelerate_value),
                                                          int(decelerate_value))

        # # 开辟进程
        # p = Process(
        #     target=lambda a: self.fixed_length_reciprocate_run(int(trip_value), int(speed_value), int(accelerate_value),
        #                                                        int(decelerate_value)))
        # p.start()
        # t = Thread(
        #     target=lambda a: self.fixed_length_reciprocate_run(int(trip_value), int(speed_value), int(accelerate_value),
        #                                                        int(decelerate_value)))
        # t.start()

        asyncio.run(
            lambda a: self.fixed_length_reciprocate_run(int(trip_value), int(speed_value), int(accelerate_value),
                                                        int(decelerate_value)))

    async def fixed_length_reciprocate_run(self, trip_value: int, speed_value: int, accelerate_value: int,
                                           decelerate_value: int):
        # motor_action = MotorAction()
        self.motor_action.fixed_length_reciprocate_motion(trip_value, speed_value, accelerate_value, decelerate_value)

    def click_emergency_stop(self):
        """
        电机急停
        :return:
        """
        # motor_action = MotorAction()
        self.motor_action.motor_emergency_stop()

    def click_forward(self):
        """
        点击右转
        :return:
        """
        # 减速度
        decelerate_value = self.decelerate_input_2.text()
        # 加速度
        accelerate_value = self.accelerate_input.text()
        # 速度
        speed = self.speed_input_2.text()

        self.motor_action.point_io_forward_rotate(int(decelerate_value), int(accelerate_value), int(speed))
        # self.motor_action.point_io_back_rotate(int(decelerate_value), int(accelerate_value), int(speed))

    def click_back(self):
        """
        点击左转
        :return:
        """
        # 减速度
        decelerate_value = self.decelerate_input_2.text().strip()
        # 加速度
        accelerate_value = self.accelerate_input.text().strip()
        # 速度
        speed = self.speed_input_2.text().strip()

        self.motor_action.point_io_back_rotate(int(decelerate_value), int(accelerate_value), int(speed))
        # self.motor_action.point_io_forward_rotate(int(decelerate_value), int(accelerate_value), int(speed))
